package com.ffyc.ssm.controller;

import com.ffyc.ssm.common.CommonResult;
import com.ffyc.ssm.modle.Admin;
import com.ffyc.ssm.modle.Role;
import com.ffyc.ssm.modle.Web;
import com.ffyc.ssm.service.RoleService;
import com.ffyc.ssm.service.WebService;
import com.ffyc.ssm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(path = "/web")
public class WebController {

    @Autowired
    WebService webService;

    /**
     * 查询网站信息
     * @param web
     * @param session
     * @return
     */
    @GetMapping(path = "/Web")
    public CommonResult login(Web web, HttpSession session){
        CommonResult<List> commonResult = null;
        try {
            List<Web> list =webService.listWeb(web);
            if (list!=null){
                session.setAttribute("web",list);
                commonResult = new CommonResult<>(200,"登录成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }
/*    @RequestMapping(path = "/save")
    public CommonResult save(Web  web){
        CommonResult<List> commonResult = null;
        try {
         webService.saveweb(web);
        commonResult = new CommonResult<>(200,"登录成功" ,null );

        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }*/

    /**
     * 文件上传保存
     */
    @RequestMapping(path = "/save")
    public  CommonResult save(@RequestParam("logoImgSrc") CommonsMultipartFile file,
                                 @RequestParam("recordation") String recordation,
                                 @RequestParam("title") String title,
                                 @RequestParam("companyName") String companyName,
                                 @RequestParam("tel") String tel){
        CommonResult<String> commonResult =null;
        File f0 =new File("D:\\UploadImg\\logoImgSrc");
        if (!f0.exists()){
            f0.mkdir();
        }
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.newFileName(oldFileName);
        File f =new File(f0,newFileName);
        try {
            file.transferTo(f);
            Map<String,String> map = new HashMap<>();
            map.put("logoImgSrc",oldFileName);
            map.put("newFileName",newFileName);
            map.put("recordation",recordation);
            map.put("title",title);
            map.put("companyName", companyName);
            map.put("tel", tel);
            webService.saveloadweb(map);
            commonResult=new CommonResult(200,"上传成功",map);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"上传失败",null);
        }
        return  commonResult;
    }

    /**
     * 删除网站信息
     */
    @GetMapping(path = "/delete")
    public  CommonResult delete(Integer id){
        CommonResult<Web> commonResult = null;
        try {
            webService.deleteWeb(id);
            commonResult = new CommonResult<Web>(201,"删除成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<Web>(500,"登录失败" , null);
        }
        return commonResult;
    }

    /**
     * 修改 存信息
     * @param id
     * @return
     */
    @GetMapping(path = "/findweb")
    public CommonResult findweb(Integer id){
        CommonResult commonResult = null;
        try {
          Web web= webService.findweb(id);
            commonResult = new CommonResult<>(200,"查询成功" , web);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    /*
     * 修改保存信息
     */
    @PostMapping(path = "/update")
    public CommonResult update(@RequestParam("logoImgSrc")CommonsMultipartFile file,
                               @RequestParam("title")String title,
                               @RequestParam("recordation")String recordation,
                               @RequestParam("companyName")String companyName,
                               @RequestParam("tel")String tel,
                               @RequestParam("id")String id){
        //CommonResult commonResult = null;
        CommonResult<String> commonResult =null;
        File f0 =new File("D:\\UploadImg\\logoImgSrc");
        if (!f0.exists()){
            f0.mkdir();
        }
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.newFileName(oldFileName);
        File f =new File(f0,newFileName);
        try {
            file.transferTo(f);
            Map<String,String> map = new HashMap();
            map.put("title",title);
            map.put("logoImgSrc",oldFileName);
            map.put("newFileName",newFileName);
            map.put("recordation",recordation);
            map.put("companyName",companyName);
            map.put("tel",tel);
            map.put("id",id);
            System.out.println(newFileName);
            webService.updateweb(map);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }


}
